package top.xxyu.quartz.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import top.xxyu.core.Msg;
import top.xxyu.quartz.dao.ScheduleTaskDao;
import top.xxyu.quartz.entity.QuartzTask;

import java.util.UUID;

@RestController
@Log
@RequestMapping(value = "/schedule")
public class ScheduleTaskController {

    /**
     * 使用HTTP动词表示增删改查资源， GET：查询，POST：新增，PUT：更新，DELETE：删除
     */
    @Autowired
    private ScheduleTaskDao scheduleTaskDao;

    @ApiOperation(value="获取所有计划任务", notes="")
    @RequestMapping(value = "/{page}/{size}", method = RequestMethod.GET)
    public Msg getTaskAll(@PathVariable int page, @PathVariable int size) {
        Msg msg = new Msg();
        Sort orders = new Sort(Sort.Direction.DESC,"id");
        Page<QuartzTask> result = scheduleTaskDao.findAll(PageRequest.of(page - 1, size, orders));
        msg.setResult(result);
        return msg;
    }

    @ApiOperation(value="添加计划任务", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cronExp", value = "cronExp", required = true, dataType = "String"),
            @ApiImplicitParam(name = "taskClass", value = "taskClass", required = true, dataType = "String"),
            @ApiImplicitParam(name = "taskName", value = "taskName", required = true, dataType = "String")})
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Msg addTask(@RequestBody QuartzTask quartzTask) {
        quartzTask.setId(UUID.randomUUID().toString());
        scheduleTaskDao.save(quartzTask);
        return Msg.success();
    }

    @ApiOperation(value="更新计划任务", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "quartzTask", value = "quartzTask", required = true, dataType = "QuartzTask")})
    @RequestMapping(value = "updateTask/{id}", method = RequestMethod.PUT)
    public Msg updateTask(@PathVariable String id, @RequestBody QuartzTask quartzTask) {
        log.info(id);
        log.info(quartzTask.toString());
        scheduleTaskDao.save(quartzTask);
        return Msg.success();
    }

    @ApiOperation(value="删除计划任务", notes="")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
    public Msg deleteTask(@PathVariable String id) {
        log.info(id);
        if(StringUtils.isNotBlank(id)){
            scheduleTaskDao.deleteById(id);
        }
        return Msg.success();
    }
}
